(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('signonController', signonController);
	signonController.$inject = [ '$rootScope', '$scope', '$q', '$locale', '$location', '$stateParams', 'Constants', 'signonService', 'storageService', 'layoutConfig',
			'authService', 'validatorConfig', 'modalService', '$ocLazyLoad', 'ajaxServiceFactory', 'SYS_PARAMS' ];
	function signonController($rootScope, $scope, $q, $locale, $location, $stateParams, Constants, signonService, storageService, layoutConfig, 
			authService, validatorConfig, modalService, $ocLazyLoad, ajaxServiceFactory, SYS_PARAMS) {
		var ctrl = this, log = $scope.loggerFactory.getLogger("signonController");
		$scope.app.settings.login = true;
		var ajaxService = ajaxServiceFactory.buildAjaxService({
			getUserStatus : {
				url : 'do/cda/dataList/userStatus',
				method : 'get'
			},
			getLocaleList : {
				url : 'do/cda/dataList/locales',
				method : 'get'
			},
			getSiteCurrencyConfig : {
				url : 'do/cda/dataList/getSiteCurrencyConfig',
				method : 'get'
			}
		});
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.user = {
			"label7": "label.signon.label7"
		};
		ctrl.user.isPasswordShow = true;//default show
		
		
		ctrl.init = function() {
			signonService.isAuthenticated().then( function() {
				$scope.go(Constants.WELCOME_URL);
			}, function() {
				authService.obtainAntiPhishing().then(function(response) {
					ctrl.isAntiPhishingEnabled = response.data.isAntiPhishingEnabled;
					ctrl.isMachineTagEnabled = response.data.isMachineTagEnabled;
					ctrl.needVerifyCode = response.data.needVerifyCode;
					if (ctrl.isAntiPhishingEnabled == true || ctrl.isMachineTagEnabled == true) {
						ctrl.user.isPasswordShow = false;//hidden
					} else {
						ctrl.user.isPasswordShow = true;//show
					}
				}, function(response) {
					log.debug("Obtain antiPhishing error : " + angular.toJson(response));
				});
			});
		};
		
		function getUserStatus() {
			var defer = $q.defer();
			ajaxService.getUserStatus().then(function(response) {
				defer.resolve(response);
			});
			return defer.promise;
		}
		function getLocaleList() {
			var defer = $q.defer();
			ajaxService.getLocaleList().then(function(response) {
				defer.resolve(response);
			});
			return defer.promise;
		}
		function getSiteCurrencyConfig() {
			var defer = $q.defer();
			ajaxService.getSiteCurrencyConfig().then(function(response) {
				defer.resolve(response);
			});
			return defer.promise;
		}
		ctrl.signon = function() {
			if (ctrl.isAntiPhishingEnabled == true || ctrl.isMachineTagEnabled == true) {
				// route to enrollment/authenWizard1/authenWizard2/authenWizardN
				authService.signup(ctrl.user).then(function(response) {
					var forwardName = response.data.forwardName;
					if (forwardName == 'waf.enrollment') {
						ctrl.enrollment(forwardName);
					} else if (forwardName == 'waf.authenWizard1') {
						ctrl.authenWizard1(forwardName);
					} else if (forwardName == 'waf.authenWizard2') {
						ctrl.authenWizard2(forwardName);
					} else if (forwardName == 'waf.authenWizardN') {
						ctrl.authenWizardN(forwardName);
					}
				}, function(response) {
					// error
					if (response.error) {
						modalService.openErrorDialogs({
							'messages' : response.error
						});
					}
				});
			} else {
				authService.signon(ctrl.user).then(successful, failed);
			}
		};
		
		ctrl.enrollment = function(forwardName) {
			// open up this enrollment init
			modalService.openCustomDialogs({
				'dialogsModule' : forwardName,
				'btnCallback' : {
					'ok' : function(enrollment) {
								authService.enroll(enrollment).then(function(response) {
									// route to authenWizard1/authenWizard2/authenWizardN
									authService.authenWizard1Route(ctrl.user).then(function(response) {
										var forwardName = response.data.forwardName;
										if (forwardName == 'waf.authenWizard1') {
											ctrl.authenWizard1(forwardName);
										} else if (forwardName == 'waf.authenWizard2') {
											ctrl.authenWizard2(forwardName);
										} else if (forwardName == 'waf.authenWizardN') {
											ctrl.authenWizardN(forwardName);
										}
									}, angular.noop);
								}, angular.noop);
							}
				}
			});
		};
		
		ctrl.authenWizard1 = function(forwardName) {
			// open up this enrollment init
			modalService.openCustomDialogs({
				'dialogsModule' : forwardName,
				'btnCallback' : {
					'ok' : function(enrollment) {
								// route to authenWizard2/authenWizardN
								authService.authenWizard2Route(ctrl.user).then(function(response) {
									var forwardName = response.data.forwardName;
									if (forwardName == 'waf.authenWizard2') {
										ctrl.authenWizard2(forwardName);
									} else if (forwardName == 'waf.authenWizardN') {
										ctrl.authenWizardN(forwardName);
									}
								}, angular.noop);
						   }
				}
			});
		};
		
		ctrl.authenWizard2 = function(forwardName) {
			// open up this enrollment init
			modalService.openCustomDialogs({
				'dialogsModule' : forwardName,
				'btnCallback' : {
					'ok' : function(wizard2Data) {
								// route to enrollment/authenWizard1
								authService.authenWizard2Submit(wizard2Data).then(function(response) {
									var forwardName = response.data.forwardName;
									if (forwardName == 'waf.authenWizard21') {
										ctrl.authenWizard21(forwardName);
									} else if (forwardName == 'waf.authenWizard22') {
										ctrl.authenWizard22(forwardName);
									} else if (forwardName == 'waf.authenWizardN') {
										ctrl.authenWizardN(forwardName);
									}
								}, angular.noop);
						   }
				}
			});
		};
		
		ctrl.authenWizard21 = function(forwardName) {
			// open up this enrollment init
			modalService.openCustomDialogs({
				'dialogsModule' : forwardName,
				'btnCallback' : {
					'ok' : function(wizard21Data) {
								// route to enrollment/authenWizard1
								authService.authenWizard21Submit(wizard21Data).then(function(response) {
									var forwardName = response.data.forwardName;
									if (forwardName == 'waf.authenWizardN') {
										ctrl.authenWizardN(forwardName);
									}
								}, function(response) {
									// error
									if (response.error) {
										modalService.openErrorDialogs({
											'messages' : response.error
										});
									}
								});
						   }
				}
			});
		};
		
		ctrl.authenWizard22 = function(forwardName) {
			// open up this enrollment init
			modalService.openCustomDialogs({
				'dialogsModule' : forwardName,
				'btnCallback' : {
					'ok' : function(wizard22Data) {
								// route to enrollment/authenWizard1
								authService.authenWizard22Submit(wizard22Data).then(function(response) {
									var forwardName = response.data.forwardName;
									if (forwardName == 'waf.authenWizardN') {
										ctrl.authenWizardN(forwardName);
									}
								}, function(response) {
									// error
									if (response.error) {
										modalService.openErrorDialogs({
											'messages' : response.error
										});
									}
								});
						   }
				}
			});
		};
		
		ctrl.authenWizardN = function(forwardName) {
			// open up this enrollment init
			modalService.openCustomDialogs({
				'dialogsModule' : forwardName,
				'btnCallback' : {
					'ok' : function(wizardNData) {
								// delete popup message 'Are you sure to leave this page?'
								$rootScope.disableUnloadCheck();
								// signon
								ctrl.user.password = wizardNData.password;
								authService.signon(ctrl.user).then(successful, failed);
						   }
				}
			});
		};

		function successful(response) {
			signonService.afterSignon(response).then(function() {
				$rootScope.user = storageService.getUserInfo();
				$rootScope.site = storageService.getSiteInfo();
				$rootScope.menus = storageService.getMenuInfo();
				$rootScope.updateTheme();
				$rootScope.initLocaleData();
				$rootScope.initNotificationData();
				$ocLazyLoad.load(
						[ 'waf/css/' + $scope.app.settings.theme + '/app.css',
						  'waf/js/' + $scope.app.settings.theme + '/test/testRouter.js',
						  'cda/js/' + $scope.app.settings.theme + '/router/pageRouter.js',
						  'cda/js/' + $scope.app.settings.theme + '/router/lookupRouter.js',
						  'cda/js/' + $scope.app.settings.theme + '/router/popupRouter.js',
						  'cda/js/' + $scope.app.settings.theme + '/test/testRouter.js',
						  'cls/js/' + $scope.app.settings.theme + '/router/pageRouter.js',
						  'cls/js/' + $scope.app.settings.theme + '/router/lookupRouter.js',
						  'cls/js/' + $scope.app.settings.theme + '/router/popupRouter.js',
						  'apf/js/' + $scope.app.settings.theme + '/router/pageRouter.js',
						  'apf/js/' + $scope.app.settings.theme + '/router/lookupRouter.js',
						  'apf/js/' + $scope.app.settings.theme + '/router/popupRouter.js',
						  'lfe/js/' + $scope.app.settings.theme + '/router/pageRouter.js',
						  'lfe/js/' + $scope.app.settings.theme + '/router/lookupRouter.js',
						  'lfe/js/' + $scope.app.settings.theme + '/router/popupRouter.js'], {
							cache : true
						}).then(function() {
					getUserStatus().then(function(response) {
						if(response && response.data){
							angular.forEach(response.data, function(data, index, array){
								SYS_PARAMS.USER_STATUS[data.value.toUpperCase()] = data.label;
							});
						}
					});

					getLocaleList().then(function(response) {
						if(response && response.data){
							angular.forEach(response.data, function(data, index, array){
								SYS_PARAMS.LOCALE_STATUS[data.value.toUpperCase()] = data.label;
							});
						}
					});
					
					getSiteCurrencyConfig().then(function(response) {
						if(response && response.data){
							angular.forEach(response.data, function(data, index, array){
								SYS_PARAMS.CCY_CONFIG[data.ccy] = data.decimalNum;
							});
						}
					});
					if ($location.state()) {
						$scope.go($location.state());
					} else if($location.url() && $location.url() != '/signon') {
						$scope.go($location.url().substr(1).replace(/[/]/g, '.'));
					} else {
						$scope.go(Constants.WELCOME_URL);
					}
				}, function(error) {
					log.error(error);
				});
			}, angular.noop);
		}

		function failed(response) {
			if (response && response.error == 'must_change_password') {
				$scope.go('waf.password', ctrl.user);
			} else {
				modalService.openErrorDialogs({
					'messages' : response.error
				});
			}
			log.debug("signon error:" + angular.toJson(response.error));
		}
	}
})(angular);
